package com.skivingcloud.admin.sys.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.constants.Constant;
import com.skivingcloud.admin.sys.entity.SysOrg;
import com.skivingcloud.admin.sys.entity.SysTenant;
import com.skivingcloud.admin.sys.entity.SysTenantMenu;
import com.skivingcloud.admin.sys.mapper.SysOrgMapper;
import com.skivingcloud.admin.sys.mapper.SysTenantMapper;
import com.skivingcloud.admin.sys.mapper.SysTenantMenuMapper;
import com.skivingcloud.admin.sys.service.SysTenantService;
import com.skivingcloud.common.utils.PageParameter;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Service
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements SysTenantService {
    @Resource
    private SysTenantMenuMapper sysTenantMenuMapper;
    @Resource
    private SysOrgMapper sysOrgMapper;
    /**
     * 查询租户信息{key:'', value:''}
     *
     * @return 租户信息{key:'', value:''}
     */
    @Override
    @Cacheable(value="skivingTenant",key="'getTenantSelectKV'")
    public List<Map<String, Object>> getTenantSelect(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(SysTenant.class).orderBy(SysTenant::getCreateTime, false);
        return this.mapper.selectListByQuery(queryWrapper).stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("key", m.getId());
            map.put("value", m.getTenantName());
            return map;
        }).toList();
    }
    
    /**
     * 分页查询
     *
     * @param parameter 分页参数
     * @return 分页结果
     */
    @Override
    public Page<SysTenant> queryByPage(PageParameter<SysTenant> parameter){
        Page<SysTenant> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        SysTenant sysTenant = parameter.getParam();
        QueryWrapper queryWrapper = QueryWrapper.create().where(" 1=1 ");
        if(sysTenant != null){
            if(StringUtils.isNotBlank(sysTenant.getTenantCode())){
                queryWrapper.and(SysTenant::getTenantCode).like(sysTenant.getTenantCode());
            }
            if(StringUtils.isNotBlank(sysTenant.getTenantName())){
                queryWrapper.and(SysTenant::getTenantName).like(sysTenant.getTenantName());
            }
        }
        queryWrapper.orderBy(" tenant_code ");
        return this.page(page, queryWrapper);
    }
    
    /**
     * 批量删除租户信息
     *
     * @param ids 租户id数字
     */
    @Override
    @CacheEvict(value="skivingTenant", allEntries = true)
    public void deleteBatch(String[] ids){
        //删除租户资源关系
        QueryWrapper tm = QueryWrapper.create().where(SysTenantMenu::getTenantId).in(Arrays.stream(ids).toList());
        sysTenantMenuMapper.deleteByQuery(tm);
        //删除租户机构(逻辑删除)
        QueryWrapper tmOrg = QueryWrapper.create().where(SysOrg::getTenantId).in(Arrays.stream(ids).toList());
        SysOrg org = new SysOrg();
        org.setOrgStatus(Constant.COMMON_STATUS_DELETED);
        sysOrgMapper.updateByQuery(org, true, tmOrg);
        this.mapper.deleteBatchByIds(Arrays.stream(ids).toList());
    }
}
